package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryL1VO;
import com.cskaoyan.bean.CategoryListVO;
import com.cskaoyan.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商城管理的商品类目
 */

@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 商品类目首页
     * @return 商品类目列表
     */
    @RequestMapping("list")
    public BaseRespVo categoryList(){
        List<CategoryListVO> categoryListVO = categoryService.allCategoryVO();
        return BaseRespVo.ok(categoryListVO);
    }

    /**
     * 商品类目一级标签
     * @return 商品类目一级标签列表
     */
    @RequestMapping("l1")
    public BaseRespVo label1List(){
        List<CategoryL1VO> categoryL1VOList = categoryService.allCategoryL1VO();
        return BaseRespVo.ok(categoryL1VOList);
    }

    /**
     * 创建新商品类目
     */
    @RequestMapping("create")
    public BaseRespVo createCategory(@RequestBody Category category){
        Category newCategory = categoryService.createCategory(category);
        return BaseRespVo.ok(newCategory);
    }

    /**
     * 更新商品类目
     */
    @RequestMapping("update")
    public BaseRespVo updateCategory(@RequestBody Category category){
        int update = categoryService.updateCategory(category);
        if (update == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("更新失败");
    }

    /**
     * 删除商品类目
     */
    @RequestMapping("delete")
    public BaseRespVo deleteCategory(@RequestBody Category category){
        int delete = categoryService.deleteCategory(category);
        if (delete == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }

}
